<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
  <title id="cj20941">CREATE VIEW</title>
  <body>
    <p id="sql_command_desc">Defines a new view.</p>
    <section id="section2">
      <title>Synopsis</title>
      <codeblock id="sql_command_synopsis">CREATE [OR REPLACE] [TEMP | TEMPORARY] [RECURSIVE] VIEW <varname>name</varname> [ ( <varname>column_name</varname> [, ...] ) ]
    [ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
    AS <varname>query</varname>
    [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]</codeblock>
    </section>
    <section id="section3">
      <title>Description</title>
      <p><codeph>CREATE VIEW</codeph> defines a view of a query. The view is not physically
        materialized. Instead, the query is run every time the view is referenced in a query. </p>
      <p><codeph>CREATE OR REPLACE VIEW</codeph> is similar, but if a view of the same name already
        exists, it is replaced. The new query must generate the same columns that were generated by
        the existing view query (that is, the same column names in the same order, and with the same
        data types), but it may add additional columns to the end of the list. The calculations
        giving rise to the output columns may be completely different.</p>
      <p>If a schema name is given then the view is created in the specified schema. Otherwise it is
        created in the current schema. Temporary views exist in a special schema, so a schema name
        may not be given when creating a temporary view. The name of the view must be distinct from
        the name of any other view, table, sequence, index or foreign table in the same schema.</p>
    </section>
    <section id="section4">
      <title>Parameters</title>
      <parml>
        <plentry>
          <pt>TEMPORARY | TEMP</pt>
          <pd>If specified, the view is created as a temporary view. Temporary views are
            automatically dropped at the end of the current session. Existing permanent relations
            with the same name are not visible to the current session while the temporary view
            exists, unless they are referenced with schema-qualified names. If any of the tables
            referenced by the view are temporary, the view is created as a temporary view (whether
              <codeph>TEMPORARY</codeph> is specified or not). </pd>
        </plentry>
        <plentry>
          <pt>RECURSIVE</pt>
          <pd>Creates a recursive view. The syntax
            <codeblock>
CREATE RECURSIVE VIEW [ <varname>schema</varname> . ] <varname>view_name</varname> (<varname>column_names</varname>) AS SELECT <varname>...</varname>;
</codeblock>
            is equivalent to
            <codeblock>
CREATE VIEW [ <varname>schema</varname> . ] <varname>view_name</varname> AS WITH RECURSIVE <varname>view_name</varname> (<varname>column_names</varname>) AS (SELECT <varname>...</varname>) SELECT <varname>column_names</varname> FROM <varname>view_name</varname>;
</codeblock>
            A view column name list must be specified for a recursive view. </pd>
        </plentry>
        <plentry>
          <pt><varname>name</varname></pt>
          <pd>The name (optionally schema-qualified) of a view to be created. </pd>
        </plentry>
        <plentry>
          <pt><varname>column_name</varname></pt>
          <pd>An optional list of names to be used for columns of the view. If not given, the column
            names are deduced from the query. </pd>
        </plentry>
        <plentry>
          <pt>WITH ( <codeph>view_option_name</codeph> [= <codeph>view_option_value</codeph>] [, ...
            ] )</pt>
          <pd>This clause specifies optional parameters for a view; the following parameters are supported:<parml>
              <plentry>
                <pt><codeph>check_option</codeph> (string)</pt>
                <pd>This parameter may be either <codeph>local</codeph> or
                  <codeph>cascaded</codeph>, and is equivalent to specifying <codeph>WITH [ CASCADED
                    | LOCAL ] CHECK OPTION</codeph> (see below). This option can be changed on
                  existing views using <codeph><xref href="ALTER_VIEW.xml#topic1">ALTER
                    VIEW</xref></codeph>. </pd>
              </plentry>
            </parml><parml>
              <plentry>
                <pt><codeph>security_barrier</codeph> (boolean)</pt>
                <pd>This should be used if the view is intended to provide row-level security.</pd>
              </plentry>
            </parml></pd>
        </plentry>
        <plentry>
          <pt><varname>query</varname></pt>
          <pd>A <codeph><xref href="./SELECT.xml#topic1" type="topic" format="dita"/></codeph> or
                <codeph><xref href="./VALUES.xml#topic1" type="topic" format="dita"/></codeph>
            command which will provide the columns and rows of the view.</pd>
        </plentry>
      </parml>
    </section>
    <section id="section5">
      <title>Notes</title>
      <p>Views in Greenplum Database are read only. The system will not allow an insert, update, or
        delete on a view. You can get the effect of an updatable view by creating rewrite rules on
        the view into appropriate actions on other tables. For more information see <codeph>CREATE
          RULE</codeph>. </p>
      <p>Be careful that the names and data types of the view's columns will be assigned the way you
        want. For example:</p>
      <codeblock>CREATE VIEW vista AS SELECT 'Hello World';</codeblock>
      <p>is bad form in two ways: the column name defaults to <codeph>?column?</codeph>, and the
        column data type defaults to <codeph>unknown</codeph>. If you want a string literal in a
        view's result, use something like:</p>
      <codeblock>CREATE VIEW vista AS SELECT text 'Hello World' AS hello;</codeblock>
      <p>Access to tables referenced in the view is determined by permissions of the view owner not
        the current user (even if the current user is a superuser). This can be confusing in the
        case of superusers, since superusers typically have access to all objects. In the case of a
        view, even superusers must be explicitly granted access to tables referenced in the view if
        they are not the owner of the view.</p>
      <p>However, functions called in the view are treated the same as if they had been called
        directly from the query using the view. Therefore the user of a view must have permissions
        to call any functions used by the view.</p>
      <p>If you create a view with an <codeph>ORDER BY</codeph> clause, the <codeph>ORDER
          BY</codeph> clause is ignored when you do a <codeph>SELECT</codeph> from the view.</p>
      <p>When <codeph>CREATE OR REPLACE VIEW</codeph> is used on an existing view, only the view's
        defining <codeph>SELECT</codeph> rule is changed. Other view properties, including
        ownership, permissions, and non-<codeph>SELECT</codeph> rules, remain unchanged. You must
        own the view to replace it (this includes being a member of the owning role).</p>
    </section>
    <section id="section6">
      <title>Examples</title>
      <p>Create a view consisting of all comedy films: </p>
      <codeblock>CREATE VIEW comedies AS SELECT * FROM films 
WHERE kind = 'comedy';</codeblock>
      <p>This will create a view containing the columns that are in the <codeph>film</codeph> table
        at the time of view creation. Though <codeph>*</codeph> was used to create the view, columns
        added later to the table will not be part of the view. </p>
      <p>Create a view that gets the top ten ranked baby names:</p>
      <codeblock>CREATE VIEW topten AS SELECT name, rank, gender, year FROM 
names, rank WHERE rank &lt; '11' AND names.id=rank.id;</codeblock>
      <p>Create a recursive view consisting of the numbers from 1 to 100: </p>
      <codeblock>CREATE RECURSIVE VIEW public.nums_1_100 (n) AS
    VALUES (1)
UNION ALL
    SELECT n+1 FROM nums_1_100 WHERE n &lt; 100;
      </codeblock>
      <p> Notice that although the recursive view's name is schema-qualified in this <codeph>CREATE
          VIEW</codeph> command, its internal self-reference is not schema-qualified. This is
        because the implicitly-created CTE's name cannot be schema-qualified. </p>
    </section>
    <section id="section7">
      <title>Compatibility</title>
      <p>The SQL standard specifies some additional capabilities for the <codeph>CREATE
          VIEW</codeph> statement that are not in Greenplum Database. The optional clauses for the
        full SQL command in the standard are: </p>
      <ul>
        <li id="cj152047"><b>CHECK OPTION</b> — This option has to do with updatable views. All
            <codeph>INSERT</codeph> and <codeph>UPDATE</codeph> commands on the view will be checked
          to ensure data satisfy the view-defining condition (that is, the new data would be visible
          through the view). If they do not, the update will be rejected.</li>
        <li id="cj152050"><b>LOCAL</b> — Check for integrity on this view. </li>
        <li id="cj152053"><b>CASCADED</b> — Check for integrity on this view and on any dependent
          view. <codeph>CASCADED</codeph> is assumed if neither <codeph>CASCADED</codeph> nor
            <codeph>LOCAL</codeph> is specified. </li>
      </ul>
      <p><codeph>CREATE OR REPLACE VIEW</codeph> is a Greenplum Database language extension. So is
        the concept of a temporary view. </p>
    </section>
    <section id="section8">
      <title>See Also</title>
      <p><codeph><xref href="./SELECT.xml#topic1" type="topic" format="dita"/></codeph>,
            <codeph><xref href="./DROP_VIEW.xml#topic1" type="topic" format="dita"/></codeph>,
            <codeph><xref href="./CREATE_MATERIALIZED_VIEW.xml" type="topic"/></codeph></p>
    </section>
  </body>
</topic>
